from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from contextlib import contextmanager
import settings


Base = declarative_base()
engine = create_engine(settings.DB_URL)

@contextmanager
def session():
    Session = sessionmaker(bind=engine)
    try:
        session = Session()
        yield session
        session.commit()
    except:
        session.rollback()
    finally:
        session.close()


class Image(Base):
    __tablename__ = "image"

    id = Column(Integer, primary_key=True)
    checksum = Column(String(48))
    name = Column(String(48))
